package com.atguigu.tingshu.album.canal;

import cn.hutool.core.util.StrUtil;
import com.alibaba.otter.canal.protocol.CanalEntry;
import com.atguigu.tingshu.common.constant.RedisConstant;
import com.atguigu.tingshu.search.client.SearchFeignClient;
import com.xpand.starter.canal.annotation.CanalEventListener;
import com.xpand.starter.canal.annotation.ListenPoint;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.List;

/**
 * CanalClientListener CanalClient监听器
 *
 * @author Petrichor
 * @version 2024/01/05 16:47
 **/
@CanalEventListener
@Slf4j
public class CanalClientListener {

    private final RedisTemplate<String,String> redisTemplate;
    private final SearchFeignClient searchFeignClient;

    @Autowired
    public CanalClientListener(RedisTemplate<String, String> redisTemplate, SearchFeignClient searchFeignClient) {
        this.redisTemplate = redisTemplate;
        this.searchFeignClient = searchFeignClient;
    }

    @ListenPoint(schema = "tingshu_album" , table = "album_info" , eventType = { CanalEntry.EventType.UPDATE , CanalEntry.EventType.DELETE })
    public void listenerAlbumInfo(CanalEntry.EventType eventType, CanalEntry.RowData rowData) {
//        List<CanalEntry.Column> beforeColumnsList = rowData.getBeforeColumnsList();
//        beforeColumnsList.forEach(column -> {
//            String name = column.getName();
//            log.info("beforeColumnsList...getBeforeColumnsList...name:{} ",name);
//            String value = column.getValue();
//            log.info("beforeColumnsList...getBeforeColumnsList...value:{} ",value);
//        });
        List<CanalEntry.Column> afterColumnsList = rowData.getAfterColumnsList();
        afterColumnsList.forEach(column -> {
            String name = column.getName();
            if (StrUtil.equalsIgnoreCase("id",name)){
                String value = column.getValue();
                redisTemplate.delete(RedisConstant.ALBUM_INFO_PREFIX+value);
                log.info("从Redis中删除了专辑数据，专辑id为: {}" , value);
            }
        });
    }
    @ListenPoint(schema = "tingshu_album",  table = "album_stat", eventType = { CanalEntry.EventType.UPDATE,CanalEntry.EventType.DELETE})
    public void listenerAlbumStat(CanalEntry.EventType eventType, CanalEntry.RowData rowData){
        List<CanalEntry.Column> afterColumnsList = rowData.getAfterColumnsList();
        // 定义变量记录更新es索引库中统计数据所需要的参数
        Long albumId = null ;
        String statType = null ;
        Integer statNum = null ;
        for (CanalEntry.Column column :afterColumnsList){
            String name = column.getName();
            String value = column.getValue();
            if (StrUtil.equalsIgnoreCase("album_id",name )){
                // 删除redis中的专辑详情数据
                redisTemplate.delete("album:stat:" + column.getValue()) ;
                log.info("从Redis中删除了专辑统计数据，专辑id为: {}" , column.getValue());
                // 记录albumId
                albumId = Long.parseLong(value) ;
            }
            if (StrUtil.equalsIgnoreCase("stat_type", name)){
                statType = value;
            }
            if (StrUtil.equalsIgnoreCase("stat_num", name)){
                statNum = Integer.parseInt(value);
            }
            // 远程调用search微服务接口，更新ES索引库中的统计数据
            // 为了降低ES的访问压力，可以对statNum值进行判断，当达到特定的值以后进行远程调用
            searchFeignClient.updateAlbumStat(albumId, statType, statNum);
        }

    }
}


